﻿using Db4objects.Db4o;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Northwind.Config;

namespace Northwind.Management.Product
{
    /// <summary>
    /// Interaction logic for UserControl_Product.xaml
    /// </summary>
    public partial class UserControl_Product : UserControl, INotifyPropertyChanged
    {
        private bool _Update = false;

        #region Constructor
        public UserControl_Product()
        {
            InitializeComponent();

            // Load Product.
            using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
            {
                dataGrid1.ItemsSource = db.Query<Northwind.Objects.Product>().ToList();
            }
            //            dataGrid1.ItemsSource = DB4O.Query<Northwind.Objects.Product>().ToList();

            //dataGrid1.DataContext = this;
            //LoadData();

            Product = new Objects.Product();
            this.DataContext = Product;
        }
        #endregion Constructor End

        #region Properties
        private Northwind.Objects.Product _Product;
        public Northwind.Objects.Product Product
        {
            get
            { return _Product; }
            set
            {
                _Product = value;
                OnPropertyChanged("Product");
            }
        }
        public IList<Northwind.Objects.Product> Products { get; set; }
        private IObjectContainer _DB4O;
        public IObjectContainer DB4O
        {
            get
            {
                if (_DB4O == null)
                    _DB4O = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName);
                return _DB4O;
            }
        }
        #endregion Properties End

        private void CopyData(Northwind.Objects.Product pProductSrc, Northwind.Objects.Product pProductDes)
        {
            pProductDes.Discontinued = pProductSrc.Discontinued;
            pProductDes.ProductID = pProductSrc.ProductID;
            pProductDes.ProductName = pProductSrc.ProductName;
            pProductDes.QuantityPerUnit = pProductSrc.QuantityPerUnit;
            pProductDes.ReorderLevel = pProductSrc.ReorderLevel;
            pProductDes.UnitPrice = pProductSrc.UnitPrice;
            pProductDes.UnitsInStock = pProductSrc.UnitsInStock;
            pProductDes.UnitsOnOrder = pProductSrc.UnitsOnOrder;
        }

        private int getID()
        {
            int result = int.Parse(DateTime.Now.ToString("ddhhmmss"));
            return result;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Objects.Product cus;
                using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
                {
                    if (_Update)
                    {
                        cus = db.Query<Northwind.Objects.Product>(p => p.ProductID == Product.ProductID).FirstOrDefault();
                        if (cus != null)
                        {
                            CopyData(Product, cus);
                            db.Store(cus);
                        }
                    }
                    else
                    {
                        Product.ProductID = getID();
                        db.Store(Product);
                    }
                }
                using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
                {
                    dataGrid1.ItemsSource = null;
                    dataGrid1.ItemsSource = db.Query<Northwind.Objects.Product>().ToList();
                }
                cus = new Objects.Product();
                CopyData(cus, Product);
                _Update = false;
            }
            catch (Exception ex)
            {
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Objects.Product cus;
                if (_Update)
                {
                    MessageBoxResult result = MessageBox.Show("Are you sure you want to remove this item ?", "Invoice", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                    if (result == MessageBoxResult.Yes)
                    {
                        using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
                        {
                            cus = db.Query<Northwind.Objects.Product>(p => p.ProductID == Product.ProductID).FirstOrDefault();
                            if (cus != null)
                            {
                                db.Delete(cus);
                            }
                        }
                        using (IObjectContainer db = Db4oEmbedded.OpenFile(DataBaseConfig.DBFileName))
                        {
                            dataGrid1.ItemsSource = null;
                            dataGrid1.ItemsSource = db.Query<Northwind.Objects.Product>().ToList();
                        }
                        cus = new Objects.Product();
                        CopyData(cus, Product);
                        _Update = false;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dataGrid1.SelectedIndex = -1;
                Objects.Product cus = new Objects.Product();
                CopyData(cus, Product);
                _Update = false;
            }
            catch (Exception ex)
            {
            }
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dataGrid1.CurrentItem != null)
            {
                Northwind.Objects.Product cus = dataGrid1.CurrentItem as Northwind.Objects.Product;
                CopyData(cus, Product);
                _Update = true;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string pPropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(pPropertyName));
            }
        }
    }
}
